package com.grape.dbmiddle.consumer.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.grape.dbmiddle.consumer.mapper.TxMiddleMapper;
import com.grape.dbmiddle.consumer.mapper.UserMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
public class ConsumerService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    TxMiddleMapper txMiddleMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Transactional
    public void addUser(String i) {
        //执行业务之前添加
        String uuid = UUID.randomUUID().toString();
        userMapper.insert("myx",26);
        //执行业务之后添加消息表数据  1已发送   2已完成
        txMiddleMapper.inset(uuid,1,i);
        //发送mq消息-需要确保消息一定发送
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("txid",uuid);
        jsonObject.put("data",i);
        amqpTemplate.convertAndSend("tx-quene-from", JSONUtil.toJsonStr(jsonObject));
    }
}
